(function() {
	function a(d, b) {
		this.image = d;
		this.options = {
			start: null,
			over: null,
			success: null,
			fail: null,
			quality: 0.7
		};
		for (var c in b) {
			this.options[c] = b[c]
		}
		if (this.options.quality > 1) {
			this.options.quality = 1
		}
		this.ret = {
			origin: null,
			base64: null,
			base64Len: null
		};
		this.init()
	}
	a.prototype = {
		constructor: a,
		init: function() {
			var b = this;
			b.create(b.image)
		},
		create: function(f) {
			var e = this,
				c = new Image(),
				d = e.ret,
				b = (typeof f === "string") ? f : URL.createObjectURL(f);
			c.crossOrigin = "*";
			c.onerror = function() {
				var g = new Error("图片加载失败");
				if (typeof e.options.fail === "function") {
					e.options.fail(g)
				}
				if (typeof e.options.over === "function") {
					e.options.over()
				}
			};
			c.onload = function() {
				var i = e.resize(this);
				var h = document.createElement("canvas");
				var g = h.getContext("2d");
				h.width = i.w;
				h.height = i.h;
				g.fillStyle = "#fffff";
				g.fillRect(0, 0, i.w, i.h);
				d.origin = f;
				g.drawImage(c, 0, 0, i.w, i.h);
				d.base64 = h.toDataURL("image/jpeg", e.options.quality);
				j(d);

				function j(k) {
					h = null;
					c = null;
					URL.revokeObjectURL(b);
					k.base64Len = k.base64.length;
					if (typeof e.options.success === "function") {
						e.options.success(k)
					}
					if (typeof e.options.over === "function") {
						e.options.over()
					}
				}
			};
			if (typeof this.options.start === "function") {
				this.options.start()
			}
			c.src = b
		},
		resize: function(c) {
			var b = this.options.width,
				e = this.options.height,
				f = c.width / c.height,
				d = {
					w: c.width,
					h: c.height
				};
			if (b & e) {
				d.w = b;
				d.h = e
			} else {
				if (b) {
					d.w = b;
					d.h = Math.ceil(b / f)
				} else {
					if (e) {
						d.w = Math.ceil(e * f);
						d.h = e
					}
				}
			}
			if (d.w >= 3264 || d.h >= 2448) {
				d.w *= 0.8;
				d.h *= 0.8
			}
			return d
		}
	};
	window.auiCompress = function(c, b) {
		return new a(c, b)
	}
})();
